package com.jiaoyou.demo.Controller;

import com.jiaoyou.demo.entity.Demand;
import com.jiaoyou.demo.service.DemandService;
import com.jiaoyou.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * 需求管理控制器
 * @author Wang
 *
 **/
@RestController
@RequestMapping("/demands")
public class demandsController {
    @Autowired
    private DemandService demandService;
    @Autowired
    private UserService userService;
    @RequestMapping("/all")
    public Object all(@RequestParam("pagenum")int page){
        int i=(page-1)*20;
        List<Demand> demands = demandService.queryAllByLimit(i,20);
        return new JsonResult(demands);
    }
    @RequestMapping("/takeorder")
    public Object takeorder(@RequestParam("demand_id")int demand_id,@RequestParam("user_id")int user_id){
        Demand demand=demandService.queryById(demand_id);
        if(user_id==demand.getUser()){
            return new JsonResult("不能接自己的单哦");
        }
        demand.setReceiver(user_id);
        demandService.update(demand);
        String contact_way=userService.queryById(user_id).getQq();
        return new JsonResult(contact_way);
    }
    @RequestMapping("/confirm")
    public Object confirm(@RequestParam("demand_id")int demand_id,@RequestParam("user_id")int user_id){
        Demand demand = demandService.queryById(demand_id);
        if(user_id==demand.getUser()){
            return new JsonResult("不能确认自己的单");
        }
            demand.setFinish(true);
            demandService.update(demand);
            return new JsonResult();

    }

}
